<template>
	<picker
		class="login-type-input"
		mode="multiSelector"
		@columnchange="bindMultiPickerColumnChange"
		:value="multiIndex"
		range-key="name"
		:range="multiArray"
	>
		{{ multiStr }}
	</picker>
</template>

<script>
/* eslint-disable */
import { countyList,townList,villageList } from '@/api/basic';
export default {
	props: {
		defaultLevel: {
			type: Number,
			default: 3
		},
		addressData: {
			type: Object,
			default() {
				return {};
			}
		}
	},
	data() {
		return {
			multiArray: [],
			multiIndex: [0, 0, 0],
			multiStr: ''
		};
	},
	computed: {  
		cityCode() {
			return this.addressData.cityCode  
		}  
	},
	watch: {
    	cityCode(newValue,oldValue) {
			this.multiIndex = [0, 0, 0];
			this.multiStr = '';
			this.getCountyList();
		}
	},
	mounted() {
		//this.$mHelper.log(this.addressData);
		//this.getCountyList();
	},
	methods: {
		async getCountyList() {
			if(this.addressData.cityCode == undefined || this.addressData.cityCode == '') {
				return;
			}
			/* let countyIndex = 0;
			let townIndex = 0;
			let villageIndex = 0;
			let county_name = null;
			let town_name = null;
			let village_name = null; */
			await this.$http.get(`${countyList}`, {
					cityCode: this.addressData.cityCode
				}).then(async r => {
				this.multiArray[0] = r;
				//console.log(this.addressData)
				if (this.addressData.countyCode) {
					r.forEach((item, index) => {
						if (
							parseInt(item.id, 10) ===
							parseInt(this.addressData.countyCode, 10)
						) {
						//console.log(this.addressData.countyCode)
							//countyIndex = index;
							this.addressData.countyCode = item.id;
							this.addressData.county = item.name;
							//county_name = item.name;
						}
					});
				} else {
					this.addressData.countyCode = this.multiArray[0][0].id;
					this.addressData.county = this.multiArray[0][0].name;
					//county_name = this.multiArray[0][0].name;
					//this.$mHelper.log(this.addressData.countyCode);
				}
				await this.$http
					.get(`${townList}`, {
						countyCode: this.addressData.countyCode
					})
					.then(async r => {
						this.multiArray[1] = r;
						if (this.addressData.townCode) {
							r.forEach((item, index) => {
								if (
									parseInt(item.id, 10) ==
									parseInt(this.addressData.townCode, 10)
								) {
									//townIndex = index;
									this.addressData.townCode = item.id;
									this.addressData.town = item.name;
									//town_name = item.name;
								}
							});
						} else {
							this.addressData.townCode = this.multiArray[1][0].id;
							this.addressData.town = this.multiArray[1][0].name;
							//town_name = this.multiArray[1][0].name;
						}
						await this.$http
							.get(`${villageList}`, {
								townCode: this.addressData.townCode
							})
							.then(r => {
								this.multiArray[2] = r;
								if (this.addressData.villageCode) {
									r.forEach((item, index) => {
										if (
											parseInt(item.id, 10) ==
											parseInt(this.addressData.villageCode, 10)
										) {
											//villageIndex = index;
											this.addressData.villageCode = item.id;
											this.addressData.village = item.name;
											//village_name = item.name;
										}
									});
								} else {
									this.addressData.villageCode = this.multiArray[2][0].id;
									this.addressData.village = this.multiArray[2][0].name;
									//village_name = this.multiArray[2][0].name;
								}
								//this.multiIndex = [countyIndex, townIndex, villageIndex];
								//this.multiStr = `${county_name}, ${town_name}, ${village_name}`;
								//this.$emit('getRegions', this.addressData);
							});
					});
			});
		},
		async bindMultiPickerColumnChange(e) {
			if(this.addressData.cityCode == '') {
				this.$mHelper.toast('请先选择城市');
				return;
			}
			this.multiIndex[e.detail.column] = e.detail.value;
			let countyIndex = 0;
			let townIndex = 0;
			let villageIndex = 0;
			let county_name = null;
			let town_name = null;
			let village_name = null;
			switch (e.detail.column) {
				case 0: //拖动第1列
					if (this.multiIndex[0] === this.multiIndex[0]) {
						this.addressData.countyCode = this.multiArray[0][e.detail.value].id;
						this.addressData.county = this.multiArray[0][e.detail.value].name;
						county_name = this.multiArray[0][e.detail.value].name;
						await this.$http
							.get(`${townList}`, {
								countyCode: this.multiArray[0][e.detail.value].id
							})
							.then(async r => {
								this.multiArray[1] = r;
								this.addressData.townCode = this.multiArray[1][0].id;
								this.addressData.town = this.multiArray[1][0].name;
								town_name = this.multiArray[1][0].name;
								countyIndex = e.detail.value;
								await this.$http
									.get(`${villageList}`, {
										townCode: this.multiArray[1][e.detail.column].id
									})
									.then(r => {
										this.multiArray[2] = r;
										this.addressData.villageCode = this.multiArray[2][0].id;
										this.addressData.village = this.multiArray[2][0].name;
										village_name = this.multiArray[2][0].name;
										this.multiIndex = [countyIndex, 0, 0];
										this.multiStr = `${county_name}, ${town_name}, ${village_name}`;
									});
							});
					}
					break;
				case 1: //拖动第2列
					if (this.multiIndex[0] === this.multiIndex[0]) {
						if (this.multiIndex[1] === this.multiIndex[1]) {
						this.addressData.countyCode = this.multiArray[0][this.multiIndex[0]].id;
						this.addressData.county = this.multiArray[0][this.multiIndex[0]].name;
						county_name = this.multiArray[0][this.multiIndex[0]].name;
						this.addressData.townCode = this.multiArray[1][e.detail.value].id;
						this.addressData.town = this.multiArray[1][e.detail.value].name;
						town_name = this.multiArray[1][e.detail.value].name;
						townIndex = e.detail.value;
						await this.$http
							.get(`${villageList}`, {
								townCode: this.multiArray[1][e.detail.value].id
							})
							.then(r => {
								this.multiArray[2] = r;
								this.addressData.villageCode = this.multiArray[2][0].id;
								this.addressData.village = this.multiArray[2][0].name;
								village_name = this.multiArray[2][0].name;
								this.multiIndex = [this.multiIndex[0], townIndex, 0];
								this.multiStr = `${county_name}, ${town_name}, ${village_name}`;
							});
						}
					}
					break;
				case 2: //拖动第3列
					if (this.multiIndex[0] === this.multiIndex[0]) {
						if (this.multiIndex[1] === this.multiIndex[1]) {
						if (this.multiIndex[2] === this.multiIndex[2]) {
							this.addressData.countyCode = this.multiArray[0][this.multiIndex[0]].id;
							this.addressData.county = this.multiArray[2][this.multiIndex[0]].name;
							county_name = this.multiArray[0][this.multiIndex[0]].name;
							this.addressData.townCode = this.multiArray[1][this.multiIndex[1]].id;
							this.addressData.town = this.multiArray[2][this.multiIndex[1]].name;
							town_name = this.multiArray[1][this.multiIndex[1]].name;
							this.addressData.villageCode = this.multiArray[2][e.detail.value].id;
							this.addressData.village = this.multiArray[2][e.detail.value].name;
							village_name = this.multiArray[2][e.detail.value].name;
							villageIndex = e.detail.value;
							this.multiIndex = [
								this.multiIndex[0],
								this.multiIndex[1],
								villageIndex
							];
							this.multiStr = `${county_name}, ${town_name}, ${village_name}`;
						}
					}
          		}
				break;
			}
			this.addressData.address=this.multiStr
			this.$emit('getRegions', this.addressData);
		}
	}
};
</script>
